﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using OpenLS.Core.Notification;

namespace OpenLS.Spreadsheet.UI
{
    class FilterColumnViewModel : ViewModelBase
    {
        private readonly FilterColumn _model;

        public FilterColumnViewModel(FilterColumn model)
        {
            _model = model;
        }

        private List<FilterValue> _values;
        public List<FilterValue> Values
        {
            get
            {
                if (_values == null)
                {
                    Dictionary<string, string> dictionary = new Dictionary<string, string>();
                    ValuesFilter ff = Model.Filter as ValuesFilter;
                    if (ff != null)
                        foreach (var item in ff.Filters)
                        {
                            dictionary.Add(item, item);
                        }

                    Debug.Write(string.Format("Start convert filterColumn {0} ... ", DateTime.Now));
                    Range r = Model.Range;
                    var allValues = GetValues();
                    var actualValues =
                        Enumerable.Distinct(Enumerable.Take(Enumerable.Where(allValues, v => v != null), 1000));
                    List<FilterValue> result = new List<FilterValue>();
                    if (ff == null)
                    {
                        foreach (object o in actualValues)
                            result.Add(new FilterValue(o.ToString(), this));
                    }

                    else
                    {
                        foreach (object o in actualValues)
                            result.Add(new FilterValue(o.ToString(), this){IsSelected = dictionary.ContainsKey(o.ToString())});
                    }
                Debug.WriteLine(string.Format(" Done {0} count = {1}", DateTime.Now, result.Count));
                    _values =  result;
                }
                return _values;
            }
        }
        IEnumerable<object>GetValues()
        {
            Range r = Model.Range;
            foreach (Cell cell in r.GetNonEmptyCells())
            {
                if (cell.Column != Model.Column)
                    continue;
                if (cell.Row == Range.TopLeftCell.Row)
                    continue;
                yield return cell.Value;
            }
              
        }
        public Range Range { get { return Model.Range; } }
        public Column Column { get { return Model.Column; } }

        public FilterColumn Model
        {
            get { return _model; }
        }

        public Filter Filter
        {
            get { return Model.Filter; }
            set { Model.Filter = value;
                _values = null;
                notifyPropertyChanged("Values");
            notifyPropertyChanged("Filter");
            }
        }

        public void Apply(FilterValue value)
        {
            ValuesFilter valuesFilter = Model.Filter as ValuesFilter;
            if (valuesFilter == null)
            {
                valuesFilter = new ValuesFilter();
                foreach (var v in Enumerable.Where(Values, v => v.IsSelected))
                    valuesFilter.Filters.Add(v.Value);
                Model.Filter = valuesFilter;
            }
            if (value.IsSelected)
            {
                if (!valuesFilter.Filters.Contains(value.Value.ToString()))
                valuesFilter.Filters.Add(value.Value.ToString());
            }
            else
            {
                while (valuesFilter.Filters.Contains(value.Value.ToString()))
                    valuesFilter.Filters.Remove(value.Value.ToString());
            }

            Model.Filter = Model.Filter;
        }
    }
}